/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip.model;

import L.L;
import java.awt.Image;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.Externalizable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author admin
 */
public class ClipboardSnapshot implements Transferable, Serializable {    //die hauptmap die übergeben wird
    private Map<DataFlavor, Object> contents = new HashMap<DataFlavor, Object>();

    public ClipboardSnapshot(Clipboard aClipboard) {
        DataFlavor[] flavors = Arrays.copyOf(aClipboard.getAvailableDataFlavors(), aClipboard.getAvailableDataFlavors().length);
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor currentFlavor = flavors[i];
            L.o("representationclass,mimetype", currentFlavor.getRepresentationClass().toString(), currentFlavor.getMimeType());
            Object currentFlavorData = null;
            try {
                currentFlavorData = aClipboard.getData(currentFlavor);
                L.o("fetched data class:", currentFlavorData.getClass().getCanonicalName());
            } catch (UnsupportedFlavorException ex) {
                Logger.getLogger(ClipboardSnapshot.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ClipboardSnapshot.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            //serialisierbare sachen die nich einfach serialisiert werden sollen
            //müssen jetz natürlich vorne hin.
            if (currentFlavorData instanceof List) {
                L.d("CURRENTFLAVORDATA IST JAVA.UTIL.LIST, CONTENTS:");
                List l = (List) currentFlavorData;
                for (Object li : l) {
                    L.d(li);
                }
            } else if (currentFlavorData instanceof Externalizable) {
                contents.put(currentFlavor, currentFlavorData);
                L.o("EXTERNIZABLE data stored for DataFlavor,stored data,representationclass,humanclass", currentFlavor.toString(), currentFlavorData.toString(), currentFlavor.getRepresentationClass().getName(), currentFlavor.getHumanPresentableName());
            } else if (currentFlavorData instanceof Serializable) {
                contents.put(currentFlavor, currentFlavorData);
                L.o("SERIALIZABLE data stored for DataFlavor,stored data,representationclass,humanclass", currentFlavor.toString(), currentFlavorData.toString(), currentFlavor.getRepresentationClass().getName(), currentFlavor.getHumanPresentableName());
            } else if (currentFlavorData instanceof InputStream) {
                L.o("CURRENTFLAVORDATA IST INPUTSTREAM!!!");
                InputStream is = new StreamContainer((InputStream) currentFlavorData);
                contents.put(currentFlavor, is);
            } else if (currentFlavorData instanceof Image) {

                L.o("CURRENTFLAVORDATA IST JAVA.AWT.IMAGE!!!");
                Image im = (Image) currentFlavorData;
                ImageIcon ii = new ImageIcon(im);
                contents.put(currentFlavor, ii);
            //contents.put(currentFlavor, new ImageContainer((InputStream) ImageIO.createImageInputStream((BufferedImage) currentFlavorData)));
            } else {
                L.o("DATA NOT STORED FOR DataFlavor,stored data,representationclass,humanclass", currentFlavor.toString(), currentFlavor.getRepresentationClass().getName(), currentFlavor.getHumanPresentableName());
            }
        }
        L.d("neuer snapshot erstellt, hashmap: " + contents.toString());
        L.o("NEUER CLIPBOARDSNAPSHOT:anzahl flavors, clipsnapshot", aClipboard.getAvailableDataFlavors().length, aClipboard.toString());
    }

    public DataFlavor[] getTransferDataFlavors() {
        return contents.keySet().toArray(new DataFlavor[contents.size()]);
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return !contents.get(flavor).equals(null);
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
        L.dd("transferdata:" + flavor.toString());
        if (contents.get(flavor) instanceof ImageIcon) {
            ImageIcon ii = (ImageIcon) contents.get(flavor);
            Image img = ii.getImage();
            return img;
        } else {
            return contents.get(flavor);
        }
    }

    @Override
    public String toString() {
        return "[ClipboardSnapshot: contents in map:" + contents.toString() + "]";
    }

}
